package com.ashes.brave.controller;

import com.ashes.belife.common.config.RedisCache;
import com.ashes.belife.common.utils.Result;
import com.ashes.brave.service.MsgSysService;
import com.ashes.oppo.po.SysAshesMsgPO;
import com.ashes.security.frame.RequestTokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author chengxuhui
 * @description 消息模组开发
 */
@Slf4j
@Api(tags = "消息模组开发")
@RestController
@RequestMapping("/ashes/brave")
public class MsgSysController {

    @Autowired
    private MsgSysService msgSysService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 新增留言信息,
     *
     * @param sysAshesMsgPO
     * @return
     */
    @ApiOperation("新增留言信息")
    @PostMapping("/addMsgInfo")
    @PreAuthorize("hasAuthority('system:msg:add')")
    public Result<Integer> addMsgInfo(HttpServletRequest request, @RequestBody SysAshesMsgPO sysAshesMsgPO) {
        BigDecimal userId = RequestTokenUtil.getUserId(request, redisCache);
        sysAshesMsgPO.setUserId(userId);
        Integer count = msgSysService.addMsgInfo(sysAshesMsgPO);
        return Result.ok(count);
    }

    /**
     * 修改留言信息
     *
     * @param sysAshesMsgPO
     * @return
     */
    @ApiOperation("修改留言信息")
    @PostMapping("/updateMsgInfo")
    @PreAuthorize("hasAuthority('system:msg:edit')")
    public Result<Integer> updateMsgInfo(@RequestBody SysAshesMsgPO sysAshesMsgPO) {
        Integer count = msgSysService.updateMsgInfo(sysAshesMsgPO);
        return Result.ok(count);
    }

    /**
     * 删除留言
     *
     * @param sysAshesMsgPO
     * @return
     */
    @ApiOperation("删除留言信息")
    @PostMapping("/deleteMsgInfo")
    @PreAuthorize("hasAuthority('system:msg:remove')")
    public Result<Integer> deleteMsgInfo(@RequestBody SysAshesMsgPO sysAshesMsgPO) {
        String messageId = sysAshesMsgPO.getMessageId().toString();
        Integer count = msgSysService.deleteMsgInfo(messageId);
        return Result.ok(count);
    }

    /**
     * 查询留言信息
     *
     * @param page
     * @param limit
     * @param sysAshesMsgPO
     * @return
     */
    @ApiOperation("查询留言信息")
    @PostMapping("/queryMsgListData/{page}/{limit}")
    @PreAuthorize("hasAuthority('system:msg:list')")
    public Result queryMsgListData(@PathVariable Integer page,
                                   @PathVariable Integer limit,
                                   @RequestBody(required = false) SysAshesMsgPO sysAshesMsgPO) {
        PageHelper.startPage(page, limit);
        List<SysAshesMsgPO> msgList = msgSysService.queryMsgListData(sysAshesMsgPO);
        PageInfo<SysAshesMsgPO> pageInfo = new PageInfo<>(msgList);
        return Result.ok(pageInfo);
    }

    /**
     * 查询留言信息通过id
     * @param id
     * @return
     */
    @ApiOperation("查询留言信息通过id")
    @PostMapping("/queryMsgInfoById/{id}")
    @PreAuthorize("hasAuthority('system:msg:list')")
    public Result queryMsgInfoById(@PathVariable String id) {
        SysAshesMsgPO sysAshesMsgPO = msgSysService.queryMsgInfoById(id);
        return Result.ok(sysAshesMsgPO);
    }
}
